# Code to replicate Table A4 in the Appendix of "Nonpartisan Ballots and the Partisanship of Who Serves in Office"
rm(list=ls())
#install.packages("flextable")
#install.packages("officer")
library(flextable)
library(officer)
library(tidyr)

# Load dataset
df.vf.26_2 <- read.csv("officials-2017_voterfile-merged_public.csv")
head(df.vf.26_2)

# Totals
nrow(df.vf.26_2) # Full list
nrow(df.vf.26_2[!is.na(df.vf.26_2$L2_type),]) # Full data
nrow(df.vf.26_2[(!is.na(df.vf.26_2$L2_type) & df.vf.26_2$coun_or_mayor==1),]) # Working data

# By quality type
table(df.vf.26_2[!is.na(df.vf.26_2$L2_type),]$L2_type)
table(df.vf.26_2[(!is.na(df.vf.26_2$L2_type) & df.vf.26_2$coun_or_mayor==1),]$L2_type)

# Create columns
table_columnn_names <- c("Observations", "Full list", "Full data", "Working data")
Col1 = c("Total", "High quality", "Medium quality", "Low quality")
Col2 = c(nrow(df.vf.26_2), rep("-", 3))
Col3 = c(nrow(df.vf.26_2[!is.na(df.vf.26_2$L2_type),]), as.numeric(table(df.vf.26_2[!is.na(df.vf.26_2$L2_type),]$L2_type)))
Col4 = c(nrow(df.vf.26_2[(!is.na(df.vf.26_2$L2_type) & df.vf.26_2$coun_or_mayor==1),]), 
         as.numeric(table(df.vf.26_2[(!is.na(df.vf.26_2$L2_type) & df.vf.26_2$coun_or_mayor==1),]$L2_type)))

# Build table
tablea4 <- cbind(Col1, Col2, Col3, Col4)
colnames(tablea4) <- table_columnn_names
ft <- flextable(data.frame(tablea4))

# Create Word document and add table
doc <- read_docx() %>%
  body_add_par("Table A4: Number of observations in each datase", style = "heading 1") %>%
  body_add_flextable(ft)

# Save the file
print(doc, target = "TableA4.docx")
